package com.example.studykt.ui


import android.Manifest
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.util.Log
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import com.example.studykt.adapter.MailAdapter
import com.example.studykt.adapter.SmsAdapter
import com.example.studykt.entity.Databus
import com.example.studykt.util.*
import kotlinx.android.synthetic.main.activity_main.*
import org.greenrobot.eventbus.EventBus
import java.lang.ref.WeakReference


class MainActivity : AppCompatActivity(), View.OnClickListener {

    companion object {
        @JvmField
        val MSG_RECEIVED_CODE = 1
    }

    var var_a: Int = 0
    var var_b: String = "我不知道"
    var var_c: Double = 10.11
    var var_d: Int = 0


    lateinit var tv_display: TextView
    lateinit var tv_display2: TextView
    lateinit var Click_effect2: TextView
    lateinit var get_sms: TextView
    lateinit var get_Mail_list: TextView


    var arrayListOne = arrayOf(10, 20, 30, 40, 50)
    var arrayListTwo = arrayOf(1, 3, 5, 7, 9)

    var arrayListThree = arrayOf(2, 'a', 3, false, 9)
    var iterator: Iterator<Any> = arrayListThree.iterator()

    var hh: String = """啊什么方面看
        |的阿萨德卡
    """.trimMargin()


    internal var Mailpermissions = arrayOf(Manifest.permission.READ_CONTACTS)
    internal var SMSpermissions = arrayOf(Manifest.permission.READ_SMS)
    internal var mPermissionList: List<String> = ArrayList()


    lateinit var mailAdapter: MailAdapter
    lateinit var smsAdapter: SmsAdapter
    var phoneAndSmsUtil: PhoneAndSmsUtil = PhoneAndSmsUtil(this)


    lateinit var smsObserver: SmsObserver


    private var smslist = ArrayList<Databus>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(com.example.studykt.R.layout.activity_main)
        Test(var_b)
        Testclick()
        xuanhuan()
    }

    fun xuanhuan() {
        for (i in 0..5) {
            Log.d("基本的遍历", i.toString())
        }

        for (j in "foanfao") {
            Log.d("遍历字符串", j.toString())
        }

        for (k in arrayListOne) {
            Log.d("遍历数组", k.toString())
        }

        for (q in arrayListTwo.indices) {
            Log.d("遍历数组的iterator", arrayListTwo[q].toString())
        }

        while (iterator.hasNext()) {
            Log.d("使用列表或数组的扩展函数遍历", iterator.next().toString())
        }


        val third = 11
        var price: String = """
            ${'$'}$third
            """.trimIndent()  //输出  $11

        //handle传递数据
        val message = Message();
        message.what = 1
        handlernew.sendMessage(message)

        val thirdhandle = thirdhandle(this)
        val message1 = Message();
        message1.what = 1
        thirdhandle.sendMessage(message1)

    }


    /**
     * 点击
     */
    fun Testclick() {
        Click_effect.setOnClickListener {
            var_a += 1
            tv_display.setText(var_a.toString())
        }

    }


    fun Test(varB: String) {

        RefreshUtils.initList(
            this, Rv_sms, RecyclerView.VERTICAL, 2,
            com.example.studykt.R.color.colorPrimary
        )
        RefreshUtils.initList(
            this, Rv_mail_list, RecyclerView.VERTICAL, 2,
            com.example.studykt.R.color.colorPrimary
        )

        tv_display = findViewById(com.example.studykt.R.id.tv_display)
        tv_display2 = findViewById(com.example.studykt.R.id.tv_display2)
        Click_effect2 = findViewById(com.example.studykt.R.id.Click_effect2)
        get_sms = findViewById(com.example.studykt.R.id.get_sms)
        get_Mail_list = findViewById(com.example.studykt.R.id.get_Mail_list)
        Click_effect2.setOnClickListener(this)
        get_sms.setOnClickListener(this)
        get_Mail_list.setOnClickListener(this)
        Monitoring_SMS.setOnClickListener(this)
        Delayed_jump.setOnClickListener(this)
    }


    /**
     * 监听事件
     */
    override fun onClick(v: View?) {

        when (v?.id) {
            com.example.studykt.R.id.Click_effect2 -> {
                var_d += 1
                tv_display2.setText(var_d.toString())
            }

            //获取手机短信
            com.example.studykt.R.id.get_sms -> {
                if (MyUtils.DetectingAddressPermissions(
                        this,
                        SMSpermissions,
                        mPermissionList
                    )
                )//检查权限
                {
                    Rv_sms.visibility = View.VISIBLE
                    Rv_mail_list.visibility = View.GONE
                    smsAdapter = SmsAdapter()
                    Rv_sms.adapter = smsAdapter
                    smsAdapter.setNewData(phoneAndSmsUtil.obtainPhoneMessage())

                }
            }

            //获取手机通讯录
            com.example.studykt.R.id.get_Mail_list -> {
                if (MyUtils.DetectingAddressPermissions(
                        this,
                        Mailpermissions,
                        mPermissionList
                    )
                )//检查权限
                {
                    Rv_sms.visibility = View.GONE
                    Rv_mail_list.visibility = View.VISIBLE
                    mailAdapter = MailAdapter()
                    Rv_mail_list.adapter = mailAdapter
                    mailAdapter.setNewData(phoneAndSmsUtil.Phone())
                }
            }


            //监听短信
            com.example.studykt.R.id.Monitoring_SMS -> {
                if (MyUtils.DetectingAddressPermissions(
                        this,
                        SMSpermissions,
                        mPermissionList
                    )
                )//检查权限
                {
                    Monitoring_SMS.text = "已开启监听"
                    smsObserver = SmsObserver(this, mHandler)
                    val uri = Uri.parse("content://sms")
                    contentResolver.registerContentObserver(uri, true, smsObserver)
                }
            }

            //延迟跳转
            com.example.studykt.R.id.Delayed_jump -> {
                handlerdelay.postDelayed(runnabledelay, 1000 * 1)
            }


        }


    }

    /**
     * 获取监听的结果
     */
    private val mHandler = object : Handler() {
        override fun handleMessage(msg: Message) {

            if (msg.what == MSG_RECEIVED_CODE) {
                val code = msg.obj as String
                Monitoring_SMS.text = code
            }
        }
    }

    /**
     * handle传递数据
     */
    var handlernew: Handler = object : Handler() {
        override fun handleMessage(msg: Message) {
            if (msg.what == 1)
                Toast.makeText(applicationContext, "handle传递数据", Toast.LENGTH_SHORT).show()
            super.handleMessage(msg)
        }
    }


    override fun onDestroy() {
        super.onDestroy()
        handlernew.removeCallbacksAndMessages(null)
    }

    /**
     * 实现延迟效果   并点击跳转
     */
    val handlerdelay: Handler = Handler()
    val runnabledelay: Runnable = Runnable {

        if (MyUtils.DetectingAddressPermissions(this, SMSpermissions, mPermissionList))//检查权限
        {
            EventBus.getDefault().postSticky("我是你爸爸")
            smslist.clear()
            smslist = phoneAndSmsUtil.obtainPhoneMessage()
            var intent = Intent(this@MainActivity, SecondActivity::class.java)
            intent.putExtra("fisrt", smslist)
            startActivity(intent)

        }

    }
}
